<?php
/**
 * 发送邮件处理
 * @author yanghj 2014.5.15
 */
class Send_emailApp extends FrontendApp
{
    var $_user_id;
    var $_email_log_mod;
    var $_user_safety_mod;
    var $_member_from;

    function __construct()
    {
        $this->Send_emailApp();
    }

    function Send_emailApp()
    {
        parent::FrontendApp();
        if($_SESSION['find_password_user_id'])
        {
            $this->_user_id = $_SESSION['find_password_user_id'];
        }
        else
        {
            $this->_user_id = intval($this->visitor->get('user_id'));           
        }
        
        $this->_member_from = isset($_GET['from']) ? trim($_GET['from']) : 'buyer';
        $this->_email_log_mod = &m("emaillog");
        $this->_user_safety_mod = &m('usersafety');
    }

    function index()
    {
        header("Location:index.php");
    }

    /**
     * 检查邮箱是否存在
     *
     */
    function chack_email()
    {
        $email = trim($_POST['email']);
        $ms = array('statu' => false, 'msg' => '');
        if (empty($this->_user_id)) {
            $this->show_warning("please_login", 'login',
                'index.php?app=member&act=login&ret_url=' . $ret_url);
            return;
        }
        if (empty($email) || !is_email($email)) {
            $ms['msg'] = '邮箱号码不正确';
            echo ecm_json_encode($ms);
            return;
        }
        $binding_email = $this->_user_safety_mod->getOne("SELECT binding_email FROM {$this->_user_safety_mod->table} WHERE user_id = {$this->_user_id} ");
        if ($email != $binding_email) {
            $ms['msg'] = '邮箱号码与绑定的邮箱号码不一致';
            echo ecm_json_encode($ms);
            return;
        } else {
            $ms['msg'] = '邮箱号码正确';
            $ms['statu'] = true;
            echo ecm_json_encode($ms);
            return;
        }

    }

    /**
     * 设置绑定邮箱
     *
     */
    function set_binding_email()
    {
        if (empty($this->_user_id)) {
            $ret_url = urlencode(get_page_url());
            $this->show_warning("please_login", 'login',
                'index.php?app=member&act=login&ret_url=' . $ret_url);
            return;
        }
        $binding_email = $this->_user_safety_mod->getOne("SELECT binding_email FROM {$this->_user_safety_mod->table} WHERE user_id = {$this->_user_id} ");
        if (!IS_POST) {
            if (!isset($_GET['id']) || !isset($_GET['activation']) || empty($_GET['activation'])) {
                $this->show_warning("request_error", 'back_index', 'index.php');
                return;
            }
            $id = intval(trim($_GET['id']));
            $activation = trim($_GET['activation']);
            $res = $this->_email_log_mod->getRow("SELECT active_code,send_email FROM {$this->_email_log_mod->table} WHERE user_id = {$this->_user_id} AND code_type=2");
            

            if ($activation != $res['active_code']) {
                $this->show_warning("该验证链接无效,请重新发送邮件验证!", 'goback',
                    'index.php?app=account_security&act=authen_verify&mode=set_email&from=' . $this->_member_from);
                return;
            }
            $this->_email_log_mod->add_edit(array('active_code' => '', 'code_type' => 2), $this->_user_id);
            $this->_user_safety_mod->add_edit(array('binding_email' => $res['send_email'],'user_id' => $this->_user_id),$this->_user_id);                  
            if ($this->_email_log_mod->has_error()) {
                $this->show_warning($this->_email_log_mod->get_error());

                return;
            }
            header("Location:index.php?app=account_security&act=authen_verify&mode=set_email&step=2&from=" . $this->_member_from);
            return;

        }

    }
    
    /**
     * 设置支付密码与找回支付密码验证邮箱
     *
     */
    function pay_password_email()
    {
        if (empty($this->_user_id)) {
            $ret_url = urlencode(get_page_url());
            $this->show_warning("please_login", 'login',
                'index.php?app=member&act=login&ret_url=' . $ret_url);
            return;
        }
        $mode = isset($_GET['mode']) ? trim($_GET['mode']) : 'set_pay_password';
        $pay_password = $this->_user_safety_mod->getOne("SELECT pay_password FROM {$this->_user_safety_mod->table} WHERE user_id = {$this->_user_id} ");
        if($mode == 'set_pay_password')
        {
            if($pay_password)
            {
            $this->show_warning("你已经设置了支付密码,不用重新设置!", 'goback',
                    'index.php?app=account_security&from=' . $this->_member_from);
                return;           
           }
            
        }              
        if ($mode) {
            if (!isset($_GET['id']) || !isset($_GET['activation']) || empty($_GET['activation'])) {
                $this->show_warning("request_error", 'back_index', 'index.php');
                return;
            }
            $id = intval(trim($_GET['id']));
            $activation = trim($_GET['activation']);
            $res = $this->_email_log_mod->getRow("SELECT active_code,send_email FROM {$this->_email_log_mod->table} WHERE user_id = {$this->_user_id} AND code_type=1");

            if ($activation != $res['active_code']) {
                $this->show_warning("该验证链接无效,请重新发送邮件验证!", 'goback',
                    'index.php?app=account_security&act=authen_verify&mode=set_pay_password&set_type=email&from=' . $this->_member_from);
                return;
            }
            $this->_email_log_mod->add_edit(array('active_code' => '', 'code_type' => 1), $this->_user_id);
            if ($this->_email_log_mod->has_error()) {
                $this->show_warning($this->_email_log_mod->get_error());

                return;
            }
            if($mode == 'set_pay_password')
            {
                $_SESSION['set_pay_password_step'] =  2;
            }
            if($mode == 'find_pay_password')
            {
                $_SESSION['find_pay_password_step'] =  2;
            }
                                    
            header("Location:index.php?app=account_security&act=authen_verify&mode=".$mode."&set_type=email&step=2&from=" . $this->_member_from);
            return;

        } 

    }        

    /**
     * 用AJAX发送邮件
     *
     */
    function ajax_send_email()
    {
        if (empty($this->_user_id)) {
            $ret_url = urlencode(get_page_url());
            $this->show_warning("please_login", 'login',
                'index.php?app=member&act=login&ret_url=' . $ret_url);
            return;
        }
        $binding_email = $this->_user_safety_mod->getOne("SELECT binding_email FROM {$this->_user_safety_mod->table} WHERE user_id = {$this->_user_id} ");
        $email = trim($_POST['email']);
        $method = trim($_POST['method']);
       // $email = '447180126@qq.com';
        //$method = 'set_binging_email';
        $ms = array('statu' => false, 'msg' => '');
        if (empty($method)) {
            $this->show_warning("request_error", 'back_index', 'index.php');
            return;                      
        }
        if (empty($email) || !is_email($email)) {
            $ms['msg'] = '邮箱号码不正确';
            echo ecm_json_encode($ms);
            return;
        }
        $word = $this->_rand();
        $active_code = md5($word);
        $data = array(
                'user_id' => $this->_user_id,
                'active_code' => $active_code,
                'code_type' => 1,
                'send_time' => gmtime(),
                'send_email' => $email,
                );
        /* 获取邮件发送接口 */
       $mailer =& get_mailer();        
        switch ($method) {
            case 'set_binging_email': //设置绑定邮箱
                
                $info = array(
                    'user_id' => $this->_user_id,
                    'user_name' => $this->visitor->get('user_name'),
                    'member_from' => $this->_member_from,
                    'active_code' => $active_code,
                    );
                $data['code_type'] = 2;    
                $mail = get_mail('touser_binding_email', array('user' => $info));                 
                $send_rs = $mailer->send($email, addslashes($mail['subject']), addslashes($mail['message']), CHARSET, 1);
                //$send_rs = $this->_mailto($email, addslashes($mail['subject']), addslashes($mail['message']));
                /*if(!$send_rs)
                {
                    $ms['msg'] = '发送邮件失败,请重新发送';
                    echo ecm_json_encode($ms);
                  return;
                }*/
                $this->_email_log_mod->add_edit($data, $this->_user_id);
                if ($this->_email_log_mod->has_error()) {
                    $ms['msg'] = '数据处理失败,请重新发送';
                  echo ecm_json_encode($ms);
                    return;
                 }
                 $ms['msg'] = '发送邮件成功';
                 $ms['statu'] = true;
                 echo ecm_json_encode($ms);
                break;
            case 'edit_binging_email': //修改绑定邮箱
                
                $info = array(
                    'user_id' => $this->_user_id,
                    'user_name' => $this->visitor->get('user_name'),
                    'member_from' => $this->_member_from,
                    'active_code' => $active_code,
                    );
                $data['code_type'] = 2;    
                $mail = get_mail('touser_binding_email', array('user' => $info));
                $send_rs = $mailer->send($email, addslashes($mail['subject']), addslashes($mail['message']), CHARSET, 1);
               // $send_rs = $this->_mailto($email, addslashes($mail['subject']), addslashes($mail['message']));
                /*if(!$send_rs)
                {
                    $ms['msg'] = '发送邮件失败,请重新发送';
                    echo ecm_json_encode($ms);
                  return;
                }*/
                $this->_email_log_mod->add_edit($data, $this->_user_id);
                if ($this->_email_log_mod->has_error()) {
                    $ms['msg'] = '数据处理失败,请重新发送';
                  echo ecm_json_encode($ms);
                    return;
                 }
                 $ms['msg'] = '发送邮件成功';
                 $ms['statu'] = true;
                 echo ecm_json_encode($ms);
                break;
           case 'set_pay_password_email': //设置支付密码
                $info = array(
                    'user_id' => $this->_user_id,
                    'user_name' => $this->visitor->get('user_name'),
                    'member_from' => $this->_member_from,
                    'active_code' => $active_code,
                    );
                                   
                $mail = get_mail('touser_set_pay_password_email', array('user' => $info));
                $send_rs = $mailer->send($email, addslashes($mail['subject']), addslashes($mail['message']), CHARSET, 1);
                //$send_rs = $this->_mailto($email, addslashes($mail['subject']), addslashes($mail['message']));
                /*if(!$send_rs)
                {
                    $ms['msg'] = '发送邮件失败,请重新发送';
                    echo ecm_json_encode($ms);
                  return;
                }*/
                $this->_email_log_mod->add_edit($data, $this->_user_id);
                if ($this->_email_log_mod->has_error()) {
                    $ms['msg'] = '数据处理失败,请重新发送';
                  echo ecm_json_encode($ms);
                    return;
                 }
                 $ms['msg'] = '发送邮件成功';
                 $ms['statu'] = true;
                 echo ecm_json_encode($ms);
                break;
           case 'find_pay_password_email': //找回支付密码
                $info = array(
                    'user_id' => $this->_user_id,
                    'user_name' => $this->visitor->get('user_name'),
                    'member_from' => $this->_member_from,
                    'active_code' => $active_code,
                    );
    
                $mail = get_mail('touser_find_pay_password_email', array('user' => $info));
                $send_rs = $mailer->send($email, addslashes($mail['subject']), addslashes($mail['message']), CHARSET, 1);
                //$send_rs = $this->_mailto($email, addslashes($mail['subject']), addslashes($mail['message']));
                /*if(!$send_rs)
                {
                    $ms['msg'] = '发送邮件失败,请重新发送';
                    echo ecm_json_encode($ms);
                  return;
                }*/
                $this->_email_log_mod->add_edit($data, $this->_user_id);
                if ($this->_email_log_mod->has_error()) {
                    $ms['msg'] = '数据处理失败,请重新发送';
                  echo ecm_json_encode($ms);
                    return;
                 }
                 $ms['msg'] = '发送邮件成功';
                 $ms['statu'] = true;
                 echo ecm_json_encode($ms);
                break;
           case 'find_password_email': //找回密码
                if($_SESSION['find_password_user_name'])
                {
                    $info = array(
                        'user_id' => $this->_user_id,
                        'user_name' => $_SESSION['find_password_user_name'],
                        'active_code' => $active_code,
                        );
                      
                    $mail = get_mail('touser_find_password', array('user' => $info));
                    $send_rs = $mailer->send($email, addslashes($mail['subject']), addslashes($mail['message']), CHARSET, 1);
                    //$send_rs = $this->_mailto($email, addslashes($mail['subject']), addslashes($mail['message']));
                    /*if(!$send_rs)
                    {
                        $ms['msg'] = '发送邮件失败,请重新发送';
                        echo ecm_json_encode($ms);
                      return;
                    }*/
                    $this->_email_log_mod->add_edit($data, $this->_user_id);
                    if ($this->_email_log_mod->has_error()) {
                        $ms['msg'] = '数据处理失败,请重新发送';
                      echo ecm_json_encode($ms);
                        return;
                     }
                     $ms['msg'] = '发送邮件成功';
                     $ms['statu'] = true;                  
                }               
                 echo ecm_json_encode($ms);
                break;               
           default:
                $ms['msg'] = '请求错误';
                $ms['statu'] = false;
                echo ecm_json_encode($ms);
                break;     
        }


    }


    /**
     * 构造15位的随机字符串
     *
     * @return string | 生成的字符串
     */
    function _rand()
    {
        $word = $this->generate_code();
        return $word;
    }

    function generate_code($len = 15)
    {
        $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        for ($i = 0, $count = strlen($chars); $i < $count; $i++) {
            $arr[$i] = $chars[$i];
        }

        mt_srand((double)microtime() * 1000000);
        shuffle($arr);
        $code = substr(implode('', $arr), 5, $len);
        return $code;
    }
}
?>
